/**
 * Copyright © 2021-2025 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.follow.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.jeeplus.aop.logging.annotation.ApiLog;
import com.jeeplus.core.excel.EasyExcelUtils;
import com.jeeplus.core.excel.ExcelOptions;
import com.jeeplus.core.excel.annotation.ExportMode;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.follow.domain.FollowUpInfo;
import com.jeeplus.follow.service.FollowUpInfoService;
import com.jeeplus.follow.service.dto.FollowUpInfoDTO;
import com.jeeplus.follow.service.mapstruct.FollowUpInfoWrapper;
import com.jeeplus.security.util.SecurityUtils;
import com.jeeplus.sys.service.dto.RoleDTO;
import com.jeeplus.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

/**
 * 回访信息Controller
 * @author xx
 * @version 2023-12-30
 */

@Api(tags ="回访信息")
@RestController
@RequestMapping(value = "/follow/followUpInfo")
public class FollowUpInfoController {

	@Autowired
	private FollowUpInfoService followUpInfoService;

	@Autowired
	private FollowUpInfoWrapper followUpInfoWrapper;

	/**
	 * 回访信息列表数据
	 */
	@ApiLog("查询回访信息列表数据")
	@ApiOperation(value = "查询回访信息列表数据")
//	@PreAuthorize("hasAuthority('follow:followUpInfo:list')")
	@GetMapping("list")
	public ResponseEntity<IPage<FollowUpInfoDTO>> list(FollowUpInfoDTO followUpInfoDTO, Page<FollowUpInfoDTO> page) throws Exception {
		List<RoleDTO> roleDTOS = UserUtils.getCurrentUserDTO().getRoleDTOList();
		if (!CollectionUtils.isEmpty(roleDTOS)) {
			if (roleDTOS.stream().anyMatch(role -> "patient".equals(role.getEnName()))) {
				followUpInfoDTO.setPatientId(UserUtils.getCurrentUserDTO().getId());
			}
		}
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (followUpInfoDTO, FollowUpInfoDTO.class);
		queryWrapper.eq("followtype", followUpInfoDTO.getFollowtype());
		IPage<FollowUpInfoDTO> result = followUpInfoService.findPage (page, queryWrapper);
		return ResponseEntity.ok (result);
	}


	/**
	 * 根据Id获取回访信息数据
	 */
	@ApiLog("根据Id获取回访信息数据")
	@ApiOperation(value = "根据Id获取回访信息数据")
//	@PreAuthorize("hasAnyAuthority('follow:followUpInfo:view','follow:followUpInfo:add','follow:followUpInfo:edit')")
	@GetMapping("queryById")
	public ResponseEntity<FollowUpInfoDTO> queryById(String id) {
		return ResponseEntity.ok ( followUpInfoService.findById ( id ) );
	}

	@GetMapping("/getMyTask")
	public ResponseEntity<String> getMyTask(String getType){
//		String loginName = SecurityUtils.getLoginName();
		String message = followUpInfoService.getMyTask(getType);
		return ResponseEntity.ok(message);
	}

	/**
	 * 保存回访信息
	 */
	@ApiLog("保存回访信息")
	@ApiOperation(value = "保存回访信息")
//	@PreAuthorize("hasAnyAuthority('follow:followUpInfo:add','follow:followUpInfo:edit')")
	@PostMapping("save")
	public  ResponseEntity <String> save(@Valid @RequestBody FollowUpInfoDTO followUpInfoDTO) {
		//新增或编辑表单保存
		FollowUpInfo followUpInfo = followUpInfoWrapper.toEntity(followUpInfoDTO);
		followUpInfo.setContent(followUpInfoDTO.getContent());
		followUpInfo.setNextStep(followUpInfoDTO.getNextStep());
		followUpInfoService.saveOrUpdate (followUpInfo);
        return ResponseEntity.ok ( "保存回访信息成功" );
	}


	/**
	 * 删除回访信息
	 */
	@ApiLog("删除回访信息")
	@ApiOperation(value = "删除回访信息")
//	@PreAuthorize("hasAuthority('follow:followUpInfo:del')")
	@DeleteMapping("delete")
	public ResponseEntity <String> delete(String ids) {
		String idArray[] = ids.split(",");
        followUpInfoService.removeByIds ( Lists.newArrayList ( idArray ) );
		return ResponseEntity.ok( "删除回访信息成功" );
	}
	
	/**
     * 导出回访信息数据
     *
     * @param followUpInfoDTO
     * @param page
     * @param response
     * @throws Exception
     */
    @ApiLog("导出回访信息数据")
//    @PreAuthorize("hasAnyAuthority('follow:followUpInfo:export')")
    @GetMapping("export")
    public void exportFile(FollowUpInfoDTO followUpInfoDTO, Page <FollowUpInfoDTO> page, ExcelOptions options, HttpServletResponse response) throws Exception {
        String fileName = options.getFilename ( );
		QueryWrapper queryWrapper = QueryWrapperGenerator.buildQueryCondition (followUpInfoDTO, FollowUpInfoDTO.class);
        if ( ExportMode.current.equals ( options.getMode ( ) ) ) { // 导出当前页数据
            
        } else if ( ExportMode.selected.equals ( options.getMode ( ) ) ) { // 导出选中数据
            queryWrapper.in ( "a.id", options.getSelectIds () );
        } else { // 导出全部数据
            page.setSize ( -1 );
            page.setCurrent ( 0 );
        }
        List<FollowUpInfoDTO> result = followUpInfoService.findPage ( page, queryWrapper ).getRecords ( );
        EasyExcelUtils.newInstance ( followUpInfoService, followUpInfoWrapper ).exportExcel ( result,  options.getSheetName ( ), FollowUpInfoDTO.class, fileName,options.getExportFields (), response );
    }

    /**
     * 导入回访信息数据
     *
     * @return
     */
//    @PreAuthorize("hasAnyAuthority('follow:followUpInfo:import')")
    @PostMapping("import")
    public ResponseEntity importFile(MultipartFile file) throws IOException {
        String result = EasyExcelUtils.newInstance ( followUpInfoService, followUpInfoWrapper ).importExcel ( file, FollowUpInfoDTO.class );
        return ResponseEntity.ok ( result );
    }

    /**
     * 下载导入回访信息数据模板
     *
     * @param response
     * @return
     */
//    @PreAuthorize ("hasAnyAuthority('follow:followUpInfo:import')")
    @GetMapping("import/template")
    public void importFileTemplate(HttpServletResponse response) throws IOException {
        String fileName = "回访信息数据导入模板.xlsx";
        List<FollowUpInfoDTO> list = Lists.newArrayList();
        EasyExcelUtils.newInstance ( followUpInfoService, followUpInfoWrapper ).exportExcel ( list,  "回访信息数据", FollowUpInfoDTO.class, fileName, null, response );
    }

}
